# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://doc.scrapy.org/en/latest/topics/item-pipeline.html
import pymysql
import redis
import pymongo

from xiaoshuo.items import XiaoshuoItem, InfoItem
from xiaoshuo.settings import REDIS_HOST, REDIS_PORT, REDIS_PASSWORD, MONGODB_HOST, MONGODB_PORT, MYSQL_HOST, \
    MYSQL_PORT, MYSQL_USER, MYSQL_PASSWD, MYSQL_DB


class XiaoshuoPipeline(object):

    def __init__(self):
        self.redis_cli = redis.Redis(host=REDIS_HOST, port=REDIS_PORT, password=REDIS_PASSWORD)
        self.mongo_cli = pymongo.MongoClient(host=MONGODB_HOST, port=MONGODB_PORT)
        self.mysql_cli = pymysql.Connect(host=MYSQL_HOST, port=MYSQL_PORT, user=MYSQL_USER,
                                         password=MYSQL_PASSWD, db=MYSQL_DB, charset='utf8')

    def __del__(self):
        self.mysql_cli.close()

    def process_info(self, item):
        self.redis_cli.lpush('new_chapter:start_urls', item['url'])
        sql = f'insert tb_book(book_name, book_type, author, book_info, status, new_chapter, timeend, img_url)' \
              f'value ("{item["book_name"]}", "{item["book_type"]}","{item["author"]}",' \
              f'"{item["info"]}","{item["status"]}", "{item["new_chapter"]}", "{item["endtime"]}",' \
              f'"{item["img"]}")'
        print(sql)
        cursor = self.mysql_cli.cursor()
        cursor.execute(sql)
        self.mysql_cli.commit()

    def process_contnet(self, item):
        self.db = self.mongo_cli[item['book_type']]
        self.db[item['book_name']].insert([{'chapter': item['chapter'], 'content': item['content']}])
        cursor = self.mysql_cli.cursor()
        cursor.execute(f'insert tb_chapter (book_name, chapter_name, content)'
                       f'value ("{item["book_name"]}", "{item["chapter"]}", "{item["content"]}")')
        self.mysql_cli.commit()

    def process_item(self, item, spider):
        if isinstance(item, XiaoshuoItem):
            self.process_contnet(item)
        elif isinstance(item, InfoItem):
            self.process_info(item)
        return item
